/*
 * UsingBinaryTree.cpp
 *
 *  Created on: Nov 10, 2010
 *      Author: jacob
 */
#include "stdafx.h"
#include "UsingBinaryTree.h"

UsingBinaryTree* UsingBinaryTree::newBinaryTreeDemo()
{
	return new UsingBinaryTree();
}

UsingBinaryTree::UsingBinaryTree()
{
	this->newLine = "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=";
	this->binaryTree = BinaryTree<int>::newTree();
}

UsingBinaryTree::~UsingBinaryTree()
{
	delete binaryTree;
}

void UsingBinaryTree::run()
{
	userInput = '0';
	cin.clear();
	cout << displayMainMenu();
	cin >> userInput;
	chooseAction();
}

void UsingBinaryTree::chooseAction()
{
	switch (userInput)
	{
	case 'a':
		addToTree();
		break;
	case 'd':
		deleteFromTree();
		break;
	case 's':
		searchTree();
		break;
	case 't':
		traverseTree();
		break;
	case 'l':
		loadTree();
		break;
	case 'v':
		saveTree();
		break;
	case 'q':
		exit(1);
		break;
	default:
		cout << "\nPlease enter correct value";
		break;
	}

	run();
}

void UsingBinaryTree::addToTree()
{
	int input;
	cout << "\n" << newLine << "\nEnter int to add to tree" << newLine;
	if (cin >> input)
	{

		Node<int>* nodeToBeAdded = Node<int>::newNodeFromData(input);

		if (binaryTree->insert(nodeToBeAdded))
		{
			cout << "\nAdded";
			cout << binaryTree->preorderTraversal();
		}
		else
		{
			cout << "\nValue was not valid, duplicates are not allowed";
			delete nodeToBeAdded;
		}
	}
	else
		cout << "\nPlease Enter an Integer Value";
}

void UsingBinaryTree::deleteFromTree()
{
	int input;
	cout << "\n" << newLine << "\nEnter int to delete from tree" << newLine;
	cin >> input;

	if (binaryTree->deleteNode(input))
	{
		cout << "\nDeleted that";
		binaryTree->preorderTraversal();
	}
	else
	{
		cout << "\nValue could not be found and deleted";
	}
}

void UsingBinaryTree::searchTree()
{
	int input;
	cout << "\n" << newLine << "\nEnter int to find from tree" << newLine;
	cin >> input;

	Node<int>* foundNode = binaryTree->retrieveNode(input);

	if (foundNode != NULL)
	{
		cout << "\nFound that Node: " << foundNode;
		binaryTree->preorderTraversal();
	}
	else
	{
		cout << "\nValue could not be found";
	}
}

void UsingBinaryTree::traverseTree()
{
	cout << "\n" << newLine
			<< "\n(i)norder, p(r)eorder, p(o)storder traversal?" << newLine;
	cin >> userInput;

	switch (userInput)
	{
	case 'r':
		cout << binaryTree->preorderTraversal();
		break;
	case 'o':
		cout << binaryTree->postorderTraversal();
		break;
	case 'i':
		cout << binaryTree->inorderTraversal();
		break;
	default:
		searchTree();
		break;
	}
}

void UsingBinaryTree::loadTree()
{
	int numberRead;

	ifstream dataFile;
	dataFile.open("input.txt");

	if (dataFile.is_open())
	{
		while (dataFile >> numberRead)
		{
			binaryTree->insert(numberRead);
		}
		cout << "\nLoaded that!";
		dataFile.close();
	}
	else
	{
		cout << "Could Not Find Information Text File";
	}
}

void UsingBinaryTree::saveTree()
{
	if (!binaryTree->isEmpty())
	{
		ofstream dataFile;
		dataFile.open("output.txt");

		if (dataFile.is_open())
		{
			dataFile << binaryTree->preorderTraversal();
			cout << "\Wrote that!";
		}
		else
		{
			cout << "Could Not Find Information Text File";
		}
	}
	else
		cout << "Tree was empty";
}

string UsingBinaryTree::displayMainMenu()
{
	return (newLine
			+ "\n(a)dd, (d)elete, (s)earch, (t)raverse, (l)oad, sa(v)e, (q)uit"
			+ newLine);
}
